<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="open('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round>
            添加面经
          </el-button>
        </div>
      </template>

      <!--
        el-table 表格组件
          data: 指定表格要渲染的数据, 必须是对象数组
        el-table-column 表格的列, 一个表示一列
          prop: 用于指定当前列渲染对象的哪个属性
          label: 表头的标题
          width: 当前列的宽度, 不写表示自适应
       -->
      <el-table
        :data="list"
        style="width: 100%">
        <el-table-column
          width="400"
          prop="stem"
          label="标题">
        </el-table-column>
        <el-table-column
          prop="creator"
          label="作者">
        </el-table-column>
        <el-table-column
          prop="likeCount"
          label="点赞">
        </el-table-column>
        <el-table-column
          prop="views"
          label="浏览数">
        </el-table-column>
        <el-table-column
          width="200"
          prop="createdAt"
          label="更新时间">
        </el-table-column>
        <el-table-column
          label="操作">
          <!-- 使用作用域插槽获取表格内传递过来的数据, 也就是当前被点击的那一条数据 -->
          <template #default="scope">
            <div class="actions">
              <i class="el-icon-view" @click="open('preview', scope.row.id)"></i>
              <i class="el-icon-edit-outline" @click="open('edit', scope.row.id)"></i>
              <i class="el-icon-delete" @click="del(scope.row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 pagination -->
      <!--
        属性:
        background: 是否需要背景
        total: 总数据量
        page-size: 每页显示多少条数据
        current-page: 当前页
        事件:
        @current-change: 当前页放生变化时, 不管是任何原因发生的变化都会触发, 会自动传入当前页码值
      -->
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="current"
        :page-size="pageSize"
        :total="total"
        @current-change="hCurrentChange">
      </el-pagination>
    </el-card>

    <!-- 抽屉组件 -->
    <el-drawer
      :before-close="handleClose"
      size="60%"
      :title="title"
      :visible.sync="isShow">
      <!-- 预览盒子 -->
      <div v-if="type === 'preview'" class="article-preview">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>

      <!-- 添加和编辑的盒子 -->
      <el-form v-else :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="标题" prop="stem">
          <el-input v-model.trim="form.stem" placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor @blur="$refs.form.validateField('content')" v-model="form.content"></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button @click="onSubmit" type="primary">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { addArticle, delArticle, getArticleDetail, getArticles, updateArticle } from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      list: [],
      total: 0,
      current: 1,
      pageSize: 10,
      isShow: false,
      type: '',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.loadData()
  },
  methods: {
    async loadData () {
      // 发请求拿数据
      const res = await getArticles({
        current: this.current,
        pageSize: this.pageSize
      })
      // 覆盖当前数据, 就实现了分页效果
      this.list = res.data.rows
      this.total = res.data.total
    },
    async del (id) {
      // console.log(id)
      // 1. 发请求传递 id
      await delArticle(id)
      // 2. 请求得到结果后提示用户
      this.$message.success('删除成功')
      // 处理最后一页, 最后一条数据删除的情况
      if (this.list.length === 1 && this.current > 1) {
        this.current--
      }
      // 3. 重新渲染
      this.loadData()
    },
    hCurrentChange (current) {
      // console.log('页码变了', current)
      this.current = current
      // 发请求获取数据
      this.loadData()
    },
    async open (type, id) {
      this.type = type
      // 打开抽屉
      this.isShow = true

      // 根据类型或 id 来判断是否要发请求回显
      // if (id) {
      //   // 回显
      // }
      if (type !== 'add') {
        // 一定是编辑和预览: 都需要回显
        const res = await getArticleDetail(id)
        // console.log(res)
        this.form.stem = res.data.stem
        this.form.content = res.data.content
        this.form.id = res.data.id
        // this.form = res.data
      }
    },
    // 点击确定按钮要提交了
    onSubmit () {
      // 1. 兜底校验
      this.$refs.form.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        if (this.type === 'add') {
          // 做添加的逻辑
          // 2. 通过后发请求
          await addArticle(this.form)
          // 3. 提示用户
          this.$message.success('添加成功')
          // 重置到第一页
          this.current = 1
        } else if (this.type === 'edit') {
          // 做编辑的逻辑
          await updateArticle(this.form)
          this.$message.success('修改成功')
        }
        // 4. 关闭抽屉并重置表单
        this.handleClose()
        // this.isShow = false
        // this.$refs.form.resetFields()
        // 5. 重新渲染
        this.loadData()
      })
    },
    // 会在每次关闭抽屉时自动执行
    handleClose () {
      // 调用 done 表示关闭抽屉
      // done()
      // 将 isShow 改成 false 也行
      this.isShow = false
      // 重置表单: 还原到初始状态
      // if (this.$refs.form) {
      //   this.$refs.form.resetFields()
      // }
      this.$refs.form && this.$refs.form.resetFields()
      // 编辑回显后清空数据
      this.form = {
        stem: '',
        content: ''
      }
    }
  },
  computed: {
    title () {
      // 标题要根据 type 计算
      if (this.type === 'add') {
        return '添加面经'
      } else if (this.type === 'edit') {
        return '修改面经'
      } else {
        return '面经预览'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
